package customerService

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/customerservicegroup"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateCustomerServiceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改客服管理
func NewUpdateCustomerServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCustomerServiceLogic {
	return &UpdateCustomerServiceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateCustomerServiceLogic) UpdateCustomerService(req *types.UpdateCustomerServiceReq) (resp *types.Empty, err error) {
	if req.Id == 0 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.CustomerService.UpdateCustomerService(l.ctx, &customerservicegroup.UpdateCustomerServiceReq{
		Id: req.Id,
		Data: &customerservicegroup.AddCustomerServiceReq{
			MerchantId:           1,                                                                         // 商户ID
			CustomerServiceType:  rpc.CustomerServiceType(req.Data.CustomerServiceType.CustomerServiceType), // 客服类型
			CommunicationType:    rpc.CommunicationType(req.Data.CommunicationType.CommunicationType),       // 通讯类型
			Title:                req.Data.Title,                                                            // 名称
			CommunicationAddress: req.Data.CommunicationAddress,                                             // 通讯地址
			Sort:                 req.Data.Sort,                                                             // 排序
			Status:               rpc.StatusType(req.Data.Status),
		},
	})

	if err != nil {
		logc.Error(l.ctx, "UpdateCustomerService err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	resp = &types.Empty{}
	return
}
